﻿using System;

namespace SharpTrainer.Algorithm
{
    public class ArrayBub
    {
        private long[] a;                 // ref to array a
        private int nElems;               // number of data items

        //constructor
        public ArrayBub(int max)
        {
            a = new long[max];                 // create the array
            nElems = 0;                        // no items yet
        }

        //put element into array
        public void insert(long value)
        {
            a[nElems] = value;
            nElems++;
        }

        // displays array contents
        public void display()
        {
            for (int j = 0; j < nElems; j++)
                Console.Write(a[j] + " ");
            Console.WriteLine();
        }

        //bubble sort algorithm
        public void bubbleSort()
        {
            int i, j;

            for (i = nElems - 1; i > 1; i--)
                for (j = 0; j < i; j++)
                    if (a[j] > a[j + 1])
                        //swap(j, j+1);    
                        swap(ref a[j], ref a[j + 1]);
        }

        private void swap(int first, int second)
        {
            long temp = a[first];
            a[first] = a[second];
            a[second] = temp;
        }

        private void swap(ref long first, ref long second)
        {
            long temp = first;
            first = second;
            second = temp;
        }
    }
}
